package com.jiutianniao.p2p.acctrans.core.trans;

import org.springframework.stereotype.Service;

import com.jiutianniao.p2p.acctrans.core.account.Account;
import com.jiutianniao.p2p.acctrans.core.account.impl.DummyAccount;
import com.jiutianniao.p2p.acctrans.core.exception.TransException;
import com.jiutianniao.p2p.base.model.Money;
import com.jiutianniao.p2p.trans.client.enums.AccountActionEnum;
import com.jiutianniao.p2p.trans.client.enums.FreezeBalanceTypeEnum;
import com.jiutianniao.p2p.trans.client.model.TransInfo;
import com.jiutianniao.p2p.trans.client.result.AccountTransResult;

@Service("unFreezeAccountBalanceTrans")
public class UnFreezeAccountBalanceTrans extends AbstractAccountTrans {

	@Override
	public Account fetchCreditAccount(TransInfo transInfo)
			throws TransException {
		return new DummyAccount();
	}

	@Override
	public Account fetchDebitAccount(TransInfo transInfo) throws TransException {
		String accountNo = transInfo.getTransAccountOut();
		return this.fetchAccount(accountNo, AccountActionEnum.UN_FREEZE,
				transInfo);
	}

	@Override
	protected AccountTransResult transProcessing(TransInfo transInfo,
			Account debitAccount, Account creditAccount) throws TransException {

		AccountTransResult result = new AccountTransResult();
		Money freezeAmount = transInfo.getTransAmount();
		FreezeBalanceTypeEnum freezeType = transInfo.getFreezeType();

		// 记交易流水
		this.recordTransLog(transInfo, debitAccount, creditAccount);
		// 冻结操作
		debitAccount.unFreeze(freezeAmount, freezeType, transInfo);

		result.setSuccess(true);
		return result;
	}

}
